package cn.yangliu.nacos.management.web.sys;

import java.util.Optional;

import cn.yangliu.jpa.handler.JpaHandler;
import cn.yangliu.nacos.comm.config.security.annotations.Authorize;
import cn.yangliu.nacos.management.entity.sys.Role;
import cn.yangliu.nacos.management.model.RoleInsertModel;
import cn.yangliu.nacos.management.model.RoleQueryModel;
import cn.yangliu.nacos.management.model.RoleUpdateModel;
import cn.yangliu.nacos.management.service.sys.RoleService;
import cn.yangliu.operation.annotation.Action;
import cn.yangliu.operation.annotation.Module;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
@Api(value = "系统角色管理", tags = "系统角色管理")
@RestController
@RequestMapping("/sys/role")
@Module("系统管理-角色管理")
public class RoleController {

    /**
     * current class instance's member.
     * The Role service.
     */
    @Autowired
    private RoleService roleService;

    /**
     * current class instance's member.
     * The Jpa handler.
     */
    private JpaHandler jpaHandler = JpaHandler.INSTANCE;

    /**
     * Insert role.
     *
     * @param model the model
     * @return the role
     */
    @PostMapping
    @ApiOperation("新增角色")
    @Authorize("role:insert")
    @Action("新增角色")
    public Role insert(@RequestBody @Validated RoleInsertModel model) {
        return roleService.insert(model);
    }

    /**
     * Update role.
     *
     * @param model the model
     * @return the role
     */
    @PutMapping
    @ApiOperation("修改角色")
    @Authorize("role:update")
    @Action("修改角色")
    public Role update(@RequestBody @Validated RoleUpdateModel model) {
        return roleService.update(model);
    }

    /**
     * Delete.
     *
     * @param id the id
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除角色")
    @Authorize("role:delete")
    @Action("删除角色")
    public void delete(@PathVariable("id") Long id) {
        roleService.deleteById(id);
    }

    /**
     * Find by id role.
     *
     * @param id the id
     * @return the role
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询角色")
    @Authorize("role:query")
    public Role findById(@PathVariable("id") Long id) {
        Optional<Role> optional = roleService.findById(id);
        return optional.orElse(null);
    }

    /**
     * Page page.
     *
     * @param model the model
     * @return the page
     */
    @GetMapping("/page")
    @ApiOperation("分页查询角色")
    @Authorize("role:query")
    public Page<Role> page(@Validated RoleQueryModel model) {
        Specification<Role> specification = jpaHandler.buildSpecification(model);
        return roleService.findAll(specification, model.build());
    }

}
